package com.example.demo

import com.example.demo.exception.CustomException
import com.example.demo.utils.JsonResult
import org.springframework.web.bind.MissingServletRequestParameterException
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.multipart.MaxUploadSizeExceededException


@ControllerAdvice
class ExceptionConfig {

    @ExceptionHandler(MaxUploadSizeExceededException::class)
    @ResponseBody
    fun returnMaxUploadSizeExceededException(): JsonResult{
        return JsonResult.fail("文件大小不能超过50KB")
    }

    @ExceptionHandler(CustomException::class)
    @ResponseBody
    fun returnCustomException(exception: CustomException): JsonResult{
        return JsonResult.fail(exception.message?:"fail")
    }

    @ExceptionHandler(MissingServletRequestParameterException::class)
    @ResponseBody
    fun returnDefaultHandlerExceptionResolver(exception: MissingServletRequestParameterException): JsonResult{
        return JsonResult.fail(exception.message)
    }

    @ExceptionHandler(RuntimeException::class)
    @ResponseBody
    fun returnDefaultHandlerExceptionResolver(exception: RuntimeException): JsonResult{
        return JsonResult.fail(exception.message?:"fail")
    }
}